/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import 'breakpoint.dart';

class BreakpointManager {
  final WidthBreakpointRange _widthBreakpointRange =
      WidthBreakpointRange(xs: 0, sm: 320, md: 600, lg: 840, xl: 1440);
  final HeightBreakpointRange _heightBreakpointRange =
      HeightBreakpointRange(sm: 0, md: 0.8, lg: 1.2);
  WidthBreakpoint _widthBreakpoint = WidthBreakpoint.sm;
  HeightBreakpoint _heightBreakpoint = HeightBreakpoint.md;

  // 获取当前宽度断点
  WidthBreakpoint getCurrentWidthBreakpoint() {
    return _widthBreakpoint;
  }

  // 获取当前宽度断点
  HeightBreakpoint getCurrentHeightBreakpoint() {
    return _heightBreakpoint;
  }

  void _updateWidthBreakpoint(double width) {
    if (width >= _widthBreakpointRange.xl) {
      _widthBreakpoint = WidthBreakpoint.xl;
    } else if (width >= _widthBreakpointRange.lg) {
      _widthBreakpoint = WidthBreakpoint.lg;
    } else if (width >= _widthBreakpointRange.md) {
      _widthBreakpoint = WidthBreakpoint.md;
    } else if (width >= _widthBreakpointRange.sm) {
      _widthBreakpoint = WidthBreakpoint.sm;
    } else {
      _widthBreakpoint = WidthBreakpoint.xs;
    }
  }

  void _updateHeightBreakpoint(double rate) {
    if (rate >= _heightBreakpointRange.lg) {
      _heightBreakpoint = HeightBreakpoint.lg;
    } else if (rate >= _heightBreakpointRange.md) {
      _heightBreakpoint = HeightBreakpoint.md;
    } else {
      _heightBreakpoint = HeightBreakpoint.sm;
    }
  }

  // 更新当前宽高的断点值
// 更新断点
  void updateBreakpoint({double width = 0, double height = 0}) {
    // 更新宽度断点
    _updateWidthBreakpoint(width);
    // 更新高度断点
    _updateHeightBreakpoint(height / width);
  }
}
